<?php
class My_Helper_Action_TaskList extends Zend_Controller_Action_Helper_Abstract 
{
	public $tasks;
	public $_service;
	
	public function __construct()
	{
		$this->tasks["delete"] = "no";
		$this->tasks["sort"] = "no";
		$this->tasks["publish"] = "no";
		$this->tasks["saveOrders"] = "no";
		$this->tasks["changeOrder"] = "no";
		$this->tasks["changeCategory"] = "no";
	}
	
	public function setService($service)
    {
    	$this->_service = $service;
    }
	
	private function getTable()
    {
    	return $this->_service->getTable();
    }
	
	public function registerTask($cmd,$params = "no")
	{
		$this->tasks[$cmd] = $params;
	}

	
	public function buildData($orderBy = "", $orderDirection="", 
		$key = "", $field = "", $params = null)
	{
		return $this->_service->buildQueryData($orderBy,$orderDirection,$key,$field,$params);
	}
	
	
	public function doTask()
	{
		$cmd = $_POST["cmd"];
		if(array_key_exists($cmd,$this->tasks))
		{
			if ( $this->tasks[$cmd] != "no") {
				return $this->$cmd ( $this->tasks[$cmd] );
			} else {
				return $this->$cmd ();
			}
		}
		
		return self::buildData();
	}
	
	public function sort()
	{
		$order = explode(";",$_POST['params']);
		$orderBy = $order[0];
		$orderDirection = $order[1];
		return self::buildData($orderBy,$orderDirection);
	}
	
	public function search($field)
	{
		$key = $_POST['search'];
		$this->_actionController->view->keySearch = $key;
		return self::buildData("","",$key,$field);
	}

	public function delete()
	{
		$chkValues = $_POST["chkBox"];
		foreach ($chkValues as $id)
		{
			$this->_service->remove($id);
		}
		return self::buildData();
	}
	
	public function publish($col = "status")
	{
		if (!My_Db::checkColumnExist ( $this->getTable(), "status" )) {
			return self::buildData ( );
		}
		
		$params = explode(";",$_POST['params']);
		$status = $params[0];
		$id = $params[1];
		$status = !($status);
		
		if(!empty($col) && !empty($id))
		{
			$row = My_Db::get($this->getTable(),$id);
			$row[$col] = $status;
			My_Db::update($this->getTable(),$row,$id);
		}
		return self::buildData();
	}
	
	public function saveOrders($col="ordering")
	{
		if (!My_Db::checkColumnExist ( $this->getTable(),$col)) {
			return self::buildData ( );
		}
		
		$items = explode ( ";", $_POST ['params'] );
		
		foreach ( $items as $item ) {
			$info = explode ( ",", $item );
			$id = $info[0];
			$order = $info[1]; 
			if(!empty($col) && !empty($id))
			{
				$row = My_Db::get($this->getTable(),$id);
				$row[$col] = $order;
				My_Db::update($this->getTable(),$row,$id);
			}
		}
		return self::buildData ( );
		
	}
	
	public function changeOrder($col="ordering")
	{
		if(!My_Db::checkColumnExist($this->getTable(),$col))
		{
			return self::buildData ( );
		}
		
		$idItems = explode ( ";", $_POST ["params"] );
		
		$row1 = My_Db::get($this->getTable(),$idItems [0]);
		$row2 = My_Db::get($this->getTable(),$idItems [1]);
		
		$tmp = $row1[$col];
		$row1[$col] = $row2[$col];
		$row2[$col] = $tmp;
		
		My_Db::update($this->getTable(),$row1,$idItems [0]);
		My_Db::update($this->getTable(),$row2,$idItems [1]);
		
		return self::buildData ($col);
	}
	
	public function changeCategory($col = "categoryId")
	{
		$categoryId = $_POST [$col];
		$this->_actionController->view->$col = $categoryId;
		return self::buildData("","","","",array($categoryId));
	}
}